package com.house.service;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.house.dao.HouseMapper;
import com.house.entity.Dict;
import com.house.entity.House;
import com.house.entity.Image;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 房屋模块服务层
 */
@Service
@Slf4j
public class HouseService extends ServiceImpl<HouseMapper, House> {


    @Autowired
    ImageService imageService;

    @Autowired
    DictService dictService;



    /**
     * 房屋图片填充
     * @param house
     */
    public void imageProcessor(House house) {
        List<Image> images = imageService.list(Wrappers.<Image>query().eq("relation_id", house.getId()).orderByAsc("create_time"));

        if(!images.isEmpty()) {
            // 选择最早上传的图片当封面
            house.setImg(images.get(0).getImg());
            house.setImgs(images.stream().map(Image::getImg).collect(Collectors.toList()));
        }
    }

    /**
     * 生成测试数据
     */
    public void createTestData() {
        List<String> position = dictService.list(Wrappers.<Dict>query().eq("code", "position")).stream().map(Dict::getValue).collect(Collectors.toList());
        List<String> orientation = dictService.list(Wrappers.<Dict>query().eq("code", "orientation")).stream().map(Dict::getValue).collect(Collectors.toList());
        List<String> type = dictService.list(Wrappers.<Dict>query().eq("code", "houseType")).stream().map(Dict::getValue).collect(Collectors.toList());
        List<String> region = dictService.list(Wrappers.<Dict>query().eq("code", "region")).stream().map(Dict::getValue).collect(Collectors.toList());
        List<String> device = dictService.list(Wrappers.<Dict>query().eq("code", "device")).stream().map(Dict::getValue).collect(Collectors.toList());
        List<String> tag = dictService.list(Wrappers.<Dict>query().eq("code", "tag")).stream().map(Dict::getValue).collect(Collectors.toList());

        Random rand = new Random();

        for(int i = 1; i<= 10; i++) {

            House house = new House();
            house.init();
            house.setStatus("1");
            house.setTitle("测试房屋" + rand.nextInt(1000000));
            house.setArea(String.valueOf(rand.nextInt(200) + 5));

            if(Integer.parseInt(house.getArea()) > 60) {
                house.setPosition("zhengzu");
            } else {
                house.setPosition(position.get(rand.nextInt(position.size())));
            }

            house.setOrientation(orientation.get(rand.nextInt(orientation.size())));
            house.setType(type.get(rand.nextInt(type.size())));
            house.setRegion(region.get(rand.nextInt(region.size())));

            house.setTag(String.join(",", randomValues(tag)));
            house.setDevice(String.join(",", randomValues(device)));

            house.setAddress(xiaoqu.get(rand.nextInt(xiaoqu.size())));
            house.setFloor(String.valueOf(rand.nextInt(23) + 1));
            house.setFloorMax(Integer.parseInt(house.getFloor()) >= 6 ? "24" : "6");
            house.setElevator(Integer.parseInt(house.getFloor()) > 6 ? "1" : "0");
            house.setYears(String.valueOf(rand.nextInt(53) + 1970));
            house.setOwnerName(name.get(rand.nextInt(name.size())));
            house.setOwnerMobile(String.valueOf(rand.nextInt(999999999) + 18000000000L));
            house.setRemark(biaoqian.get(rand.nextInt(biaoqian.size())));

            super.saveOrUpdate(house);
        }
    }


    private List<String> randomValues(List<String> list) {
        Random rand = new Random();
        Set<String> res = new HashSet<>();
        int count = rand.nextInt(list.size());
        for (int i = 0; i <= count; i++) {
            res.add(list.get(rand.nextInt(list.size())));
        }
        return new ArrayList<>(res);
    }


    public static List<String> name = Arrays.asList(
            "张三",
            "李四",
            "王五",
            "赵六",
            "王女士",
            "刘女士",
            "王先生",
            "赵女士",
            "李先生"
    );
    public static List<String> xiaoqu = Arrays.asList(
            "百度城",
            "山水家园",
            "恒大",
            "竹岛天恒龙泽府",
            "望岛四十大道",
            "长峰商圈",
            "完美家园",
            "蓝星万象城",
            "小城故事",
            "侨乡国际公寓",
            "银丰工具",
            "华发九龙湾",
            "华润威海湾九里",
            "张村西海名居",
            "寨子"
    );


    public static List<String> biaoqian = Arrays.asList(
            "精装民用水电",
            "小高层南北通透出租带精装修",
            "索菲亚定制 卡萨帝空调",
            "104平精装三室拎包入住",
            "万达广场 复式 上跃",
            "免费车位到七月底！！长峰商圈！！交通便利！！",
            "精装两室，带电梯怡安苑，近万象城馨安苑尚海湾，长租优惠！",
            "精装修带家具家电齐全拎包入住",
            "两室精装修 家电齐全 天东丰禾 南曲阜 蒿泊",
            "可长租 可短租1室 大润发楼上 中信大厦北竹岛"
    );
}
